/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package run;

import com.thoughtworks.xstream.XStream;
import common.Hogar;
import common.OFAD;
import common.Ropa;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jfuentes
 */
public class XMLParse {

    public static void main(String[] args){
        //escribir();
        leer();
    }
    private static void escribir(){
        FileWriter out = null;
        try {
            XStream xstream = new XStream();
            //xstream.autodetectAnnotations(true);
           // xstream.addDefaultImplementation(Ropa.class, Articulo.class);
            //xstream.addDefaultImplementation(Hogar.class, Articulo.class);
            xstream.processAnnotations(OFAD.class);
            OFAD ofad = new OFAD();
            ofad.setIdCasaCentral(1);
            ofad.setNroOfad(2);
            Ropa ropa = new Ropa();
            ropa.setColor("color");
            ropa.setDescripcion("Descripcion ropa");
            ropa.setLinea("Linea");
            ropa.setOrigen("Arg.");
            
            ropa.setPrecioOferta(6.3);
            ropa.setPrecioUnitario(9.9);
            ropa.setReferencia(11111);
            ropa.setStock(50);
            ropa.setTalle("G");
            ofad.addArticulo(ropa);
            Hogar hogar = new Hogar();
            hogar.setColor("colorh");
            hogar.setDescripcion("Descripcion h");
            hogar.setLinea("Lineah");
            
            hogar.setPrecioOferta(6.3);
            hogar.setPrecioUnitario(9.9);
            hogar.setReferencia(11111);
            hogar.setStock(50);
            hogar.setCategoria("CAt");
            hogar.setComposicion("COmposicion");
            hogar.setMedidas("Mediasxlong");
            hogar.setNombre("NOMBRE HOGAR");
            ofad.addArticulo(hogar);
            String salida = xstream.toXML(ofad);
            out = new FileWriter(new File("./xml/salida.xml"));
            out.write(salida);
        } catch (IOException ex) {
            Logger.getLogger(XMLParse.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(XMLParse.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }
    private static void leer(){
        try{
        String datoXML = obtenerContenido("./xml/salida.xml");
        XStream xstream =  new XStream();
        //xstream.autodetectAnnotations(true);
        //xstream.addDefaultImplementation(Ropa.class, Articulo.class);
        xstream.processAnnotations(OFAD.class);

        /*xstream.alias("ofad", OFAD.class);
        xstream.alias("articulo", List.class);
        xstream.alias("ropa", Ropa.class,Articulo.class);
        xstream.alias("hogar", Hogar.class,Articulo.class);
        //xstream.alias("articulo", ArrayList.class);
*/
        //Indico el alias de las clases involucradas

        //Se obtiene el objeto "saco" a partir de su XML

        OFAD ofad = (OFAD)xstream.fromXML(datoXML);
            System.out.println("");
        }catch(Exception e){
            System.out.println(""+e.getMessage());
            e.printStackTrace();

        }
    }
    private static String obtenerContenido(String path) throws FileNotFoundException, IOException {
        StringBuffer contenido = new StringBuffer();
        File file = new File(path);
        if(file.exists()){
            FileInputStream fileInputStream = new FileInputStream(path);
            DataInputStream input = new DataInputStream(fileInputStream);
            while (input.available() !=0){
                contenido.append(input.readLine());
            }
            input.close();
        }
        return contenido.toString();
    }
}
